package cn.elricyu.kf.service;

import java.util.List;

import cn.elricyu.kf.bean.User;
import cn.elricyu.kf.dao.UserDAO;
import cn.elricyu.kf.exception.ServiceException;

public class UserService {

	private UserDAO userDao;

	public User SaveOrUpdateUser(User user) throws ServiceException {
		try {
			if (null == user.getId())
				userDao.SaveUser(user);
			else
				userDao.UpdateUser(user);
		} catch (Exception e) {
			throw new ServiceException("UserService.SaveOrUpdateUser", e);
		}
		return user;
	}

	public void deleteUser(Long id) throws ServiceException {
		try {
			userDao.deleteUser(id);
		} catch (Exception e) {
			throw new ServiceException("UserService.deleteUser", e);
		}
	}

	public List<User> getAllUsers() throws ServiceException {
		List<User> users;
		try {
			users = userDao.getAllUsers();
		} catch (Exception e) {
			throw new ServiceException("UserService.getAllUsers", e);
		}

		return users;
	}

	public User getUser(Long id) throws ServiceException {
		User user;
		try {
			user = userDao.getUser(id);
		} catch (Exception e) {
			throw new ServiceException("UserService.getUser", e);
		}
		return user;
	}

	public User getUserByUserId(String userId) throws ServiceException {
		User user;
		try {
			user = userDao.getUserByUserId(userId);
		} catch (Exception e) {
			throw new ServiceException("UserService.getUser", e);
		}
		return user;
	}

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.userDao = userDao;
	}

}
